package com.gpay.erp.services.system.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import com.gpay.erp.core.dao.BaseDao;
import com.gpay.erp.core.dao.page.PagerModel;
import com.gpay.erp.core.system.bean.Dept;
import com.gpay.erp.core.system.bean.SysUser;
import com.gpay.erp.services.system.SysUserInteface;
import org.springframework.stereotype.Service;

/**
 * 用户业务逻辑实现类
 * 
 * @author lg
 * 
 */
@Service
public class SysUserService implements SysUserInteface {
	@Resource
	private BaseDao dao;

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}

	public int insert(PagerModel e) {
		return dao.insert("sysUser.add", e);
	}

	public int delete(PagerModel e) {
		return 0;
	}

	public int deletes(String[] ids) {
		if (ids != null) {
			for (String id : ids)// 遍历id
			{
				if(id.equals("1")){
					return 0;
				}
				dao.delete("sysUser.deleteByUserId", id);
				dao.delete("sysUser.deleteById", id);// 数据库数据删除
			}
			return 1;
		}
		return 0;
	}

	public int update(PagerModel e) {
		return dao.update("sysUser.update", e);
	}

	public PagerModel selectOne(PagerModel e) {
		return (PagerModel) dao.selectOne("sysUser.selectUserNoAndUserName", e);
	}

	public PagerModel selectById(String id) {
		return null;
	}

	public PagerModel selectPageList(PagerModel e) {
		return dao.selectPageList("sysUser.selectPageList",
				"sysUser.selectPageCount", e);
	}

	public List selectList(PagerModel e) {
		return dao.selectList("sysUser.selectAll");
	}

	public int insertRoleUser(SysUser e) {
		if (e.getUserId() != null) {
			dao.delete("sysUser.deleteByUserId", e);
			for (String roleId : e.getRoleIds()) {
				e.setRoleId(roleId);
				dao.insert("role.addRoleUser", e);
			}
			return 1;
		}
		return 0;
	}

	public List findRoles(SysUser e) {
		return dao.selectList("sysUser.selectRoleIds", e);
	}

	public int update(String[] ids,String name) {
			SysUser sysUser=new SysUser();
			if(ids!=null&&name!=null){
			for (String id : ids) {
			/* 设置日期格式 */
		    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			/* 获取当前是时间 */
		    sysUser.setCreatedate(df.format(new Date()));
		    sysUser.setId(id);
		    sysUser.setStatus("1");
		    sysUser.setCreateName(name);
			dao.update("sysUser.update", sysUser);
			}
			return 1;
		}
			return 0;			
	}
}
